package com.zyf.mapreduce.partitioner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author Malegod_xiaofei
 * @create 2024-07-21-17:22
 * KEYIN, reducer 阶段输入的 key 的类型：Text
 * VALUEIN, reducer 阶段输入的 value 类型：IntWritable
 * KEYOUT, reducer 阶段输出的 key 类型： Text
 * VALUEOUT reducer 阶段输出的 value 类型： IntWritable
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private IntWritable outV = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int sum = 0;
        // ayguigu,(1,1)
        // ss,(1,1)
        for (IntWritable value : values) {
            sum += value.get();
        }
        outV.set(sum);
        // 写出
        context.write(key, outV);
    }
}
